package com.jie.commonlib.network.bean

import androidx.annotation.Keep
import com.jie.commonlib.network.http.HttpCommonConstant

/**
 * 接口返回信息实体
 *
 * @author ZhangJun
 * @date 2017/10/20
 */
@Keep
class ResponseBody<E> {
    /** 错误码  */
    var code = 0

    /** 错误信息  */
    var msg: String? = null

    /** 数据实体  */
    var data: E? = null
        private set

    /** 是否成功  */
    var isSuccess = false

    /** 是否失败  */
    var isFail = false

    constructor() {}
    constructor(code: Int, msg: String?, data: E, success: Boolean, fail: Boolean) {
        this.code = code
        this.msg = msg
        this.data = data
        isSuccess = success
        isFail = fail
    }

    fun setData(data: E) {
        this.data = data
    }

    /**
     * 判断该次网络请求是否成功
     * 由于success字段无法表示，故用这个取代
     */
    fun requestIsSuccess(): Boolean {
        return this.code == HttpCommonConstant.HTTP_REQUEST_SUCCESS_CODE
    }

    override fun toString(): String {
        return "ResponseBody{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", success=" + isSuccess +
                ", fail=" + isFail +
                '}'
    }
}